package com.marvels.common.utils;

import java.math.BigInteger;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * 生成请求编号
 * @author tangpw
 *
 */
public class GenerateReqNoUtils {
	
	/**
	 * 生成序列号
	 * @param sign
	 * @return
	 */
	public static String transctionReq(String sign) {
		StringBuffer sb = new StringBuffer();
		GregorianCalendar gc = new GregorianCalendar();
		Date date = new Date();
		gc.setTime(date);
		Random r = new Random();
		sb.append(sign).append(gc.getTimeInMillis()).append(formatStr(
				String.valueOf(new BigInteger(165, r).toString(10).toUpperCase().substring(0, 8)), 8, true, "0"));
		return sb.toString();
	}
	
	
	
	/**
	 * 生成序列号
	 * @param sign
	 * @return
	 */
	public static String transctionReq2(String sign,Integer length) {
		StringBuffer sb = new StringBuffer();
		GregorianCalendar gc = new GregorianCalendar();
		Date date = new Date();
		gc.setTime(date);
		Random r = new Random();
		sb.append(sign).append(gc.getTimeInMillis())
		.append(String.valueOf(new BigInteger(165, r).toString(10).toUpperCase().substring(0, length-sb.length()>0?length-sb.length():0)));
		return sb.toString();
	}
	
	/**
     * 格式化字符串
     * @param str
     * @param length
     * @param isBefore
     * @param str1
     * @return
     */
    public static String formatStr(String str, int length, boolean isBefore,
                                   String str1) {
        int l = length - str.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < l; i++) {
            sb.append(str1);
        }
        if (isBefore == true) {
            return sb.toString() + str;
        } else {
            return str + sb.toString();
        }
    }
    
    
}
